﻿using EFCore_CodeFirst_ForDocker_MVC.Contexts;
using EFCore_CodeFirst_ForDocker_MVC.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace EFCore_CodeFirst_ForDocker_MVC.Controllers
{
    public class fordocker : Controller
    {
        public DockerDbContext dockerdbcontext;

        public fordocker(DockerDbContext dockerdbcontext)
        {
            this.dockerdbcontext = dockerdbcontext;
        }



        // 列表展示
        public ActionResult Index()
        {
            //查询数据
            List<AskForm> aflists = dockerdbcontext.askForms.ToList();
            return View(aflists);
        }

        // GET: fordocker/Details/5
        public ActionResult Details(int id)
        {
            AskForm af = dockerdbcontext.askForms.Where(x => x.Id == id).FirstOrDefault();
            return View(af);
        }

        // 创建显示页面
        public ActionResult Create()
        {
            return View();
        }

        // 实现添加数据
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(AskForm askform)
        {
            try
            {
                //实现添加商品
                dockerdbcontext.askForms.Add(askform);
                //保存到数据库
                dockerdbcontext.SaveChanges();
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: fordocker/Edit/5
        public ActionResult Edit(int id)
        {
            AskForm af = dockerdbcontext.askForms.Where(x => x.Id == id).FirstOrDefault();
            return View(af);
        }

        // POST: fordocker/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, AskForm askform)
        {
            try
            {
                //实现添加商品
                dockerdbcontext.askForms.Update(askform);
                //保存到数据库
                dockerdbcontext.SaveChanges();
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: fordocker/Delete/5
        public ActionResult Delete(int id)
        {
            dockerdbcontext.Remove(new AskForm() { Id = id });
            dockerdbcontext.SaveChanges();
            return View();
        }

        // POST: fordocker/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}
